package com.gw.yunke.service.impl;

import com.gw.yunke.dao.NoteMapper;
import com.gw.yunke.domain.Note;
import com.gw.yunke.domain.NoteExample;
import com.gw.yunke.service.INoteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoteServiceImpl implements INoteService{
  @Autowired
  private NoteMapper noteMapper;

  @Override
  public int saveNote(Note addNote) {
    NoteExample noteExample = new NoteExample();
    NoteExample.Criteria criteria = noteExample.createCriteria();
    criteria.andCourseIdEqualTo(addNote.getCourseId());
    criteria.andUserIdEqualTo(addNote.getCourseId());
    List<Note> noteList = noteMapper.selectByExample(noteExample);
    int result = 0;
    if(noteList.size() > 0) {
      Note note = noteList.get(0);
      note.setContent(addNote.getContent());
      result = noteMapper.updateByPrimaryKey(note);
    }else{
      result = noteMapper.insertSelective(addNote);
    }
    return result;
  }

  @Override
  public Note getNote(Long courseId, Long userId) {
    NoteExample noteExample = new NoteExample();
    NoteExample.Criteria criteria = noteExample.createCriteria();
    criteria.andUserIdEqualTo(userId);
    criteria.andCourseIdEqualTo(courseId);
    List<Note> noteList = noteMapper.selectByExample(noteExample);
    if(noteList.size() > 0){
      return noteList.get(0);
    }else{
      Note note = new Note();
      note.setCourseId(courseId);
      note.setUserId(userId);
      note.setContent("");
      return note;
    }
  }
}
